import pandas as pd
import os,random,sys,codecs,datetime
from datetime import date
import random


def ReadFile(filePath):
    with codecs.open(filePath, "r") as f:
        return f.read()
def WriteFile(filePath, u, encoding="utf-8"):
    # with codecs.open(filePath,"w",encoding) as f:
    with codecs.open(filePath, "wb") as f:
        f.write(u.encode(encoding, errors="ignore"))
def CSV_2_UTF8(src, dst):
    content = ReadFile(src)
    WriteFile(dst, content, encoding="utf-8")



#获取本地路径下的基础数据
def joinPath(fileName):
    pyPath=os.path.dirname(os.path.realpath(sys.argv[0]))
    filePath=os.path.join(pyPath,fileName)
    return filePath

#  指定目录下,将数据带时间后缀保存为csv格式
def saveCsvByTime(fileNanme,saveData,TimeFlag):
    # OQPath:导出目录,fileNanme：文件名前缀,saveData：保存的数据
    now = datetime.datetime.now()
    if TimeFlag==True:
        filename = f"{fileNanme}_{now.strftime('%Y-%m-%d_%H-%M-%S')}.csv"
    else:
        filename = f"{fileNanme}.csv"
    full_path = joinPath(filename)
    saveData.to_csv(full_path,index=False)
# 按交易单位格式化
def  每手取整(代码,数量,仓位):
    if 代码[0:2]!="68":
        取整数量=round(数量*仓位,-2)
    elif 代码[0:2]=="68":
        取整数量=round(数量*仓位,0)
    return 取整数量

#加载任务配置列表
pyPath=os.path.dirname(os.path.realpath(sys.argv[0]))
print(f"当前执行任务的目录为：{pyPath}")
资金账号数据=pd.read_excel(os.path.join(pyPath,"资金账号管理.xls"))
资金账号数据=资金账号数据.fillna('')
资金账号数据["开通QND-T0"]= "N"
资金账号数据["开通HJ-T0"]="N"
资金账号数据["开通YL-T0"]="N"
资金账号数据["开通YR-T0"]="N"
资金账号数据["开通非凸拆单"]="N"
资金账号数据["开通卡方拆单"]="N"
资金账号数据["开通启能达拆单"]="N"
资金账号数据["开通跃然拆单"]="N"
资金账号数据.loc[资金账号数据['算法权限'].str.contains('QND-T0'), '开通QND-T0'] = 'Y'
资金账号数据.loc[资金账号数据['算法权限'].str.contains('HJ-T0'), '开通HJ-T0'] = 'Y'
资金账号数据.loc[资金账号数据['算法权限'].str.contains('YL-T0'), '开通YL-T0'] = 'Y'
资金账号数据.loc[资金账号数据['算法权限'].str.contains('YR-T0'), '开通YR-T0'] = 'Y'
资金账号数据.loc[资金账号数据['算法权限'].str.contains('FT-WAP'), '开通非凸拆单'] = 'Y'
资金账号数据.loc[资金账号数据['算法权限'].str.contains('KF-TWAP-PLUS'), '开通卡方拆单'] = 'Y'
资金账号数据.loc[资金账号数据['算法权限'].str.contains('QND-TWAP'), '开通启能达拆单'] = 'Y'
资金账号数据.loc[资金账号数据['算法权限'].str.contains('YR-TWAP'), '开通跃然拆单'] = 'Y'



资金账号数据=资金账号数据[资金账号数据["状态"]=="正常"]
导出数据=资金账号数据[["资金账号","账号类型","产品名称","所属机构","开通QND-T0","开通HJ-T0","开通YL-T0","开通YR-T0","开通非凸拆单","开通卡方拆单","开通启能达拆单","开通跃然拆单"]]
导出数据.to_csv(os.path.join(pyPath,"算法权限.csv"),index=False)
print("生成算法权限数据")

